﻿Public Class nuevo_estudio
    Dim id As String
    Dim bod_id As String
    Dim qry As String
    Dim flag As Boolean = False
    Dim ceditar As String
    Dim dataset As New DataSet
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
    Public Sub setid(ByVal id As Integer, ByVal ceditar As String)
        Me.id = id
        Me.ceditar = ceditar
        flag = True
    End Sub
    Private Sub nuevo_estudio_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Icon = (My.Resources.favicon)
        If (flag) Then
            Me.Text = "Editar Estudio"
            Label14.Visible = True
            RadioButton1.Visible = True
            RadioButton2.Visible = True
            RadioButton1.Checked = True
            subtitulo.Text = "Editar Estudio"
            btn_aceptar.Hide()
            btn_editar.Show()
            'carga
            ceditar = "select * from estudios"
            ceditar &= "  where est_id='" & id & "'"
            Consulta(ceditar, dataset)
            TextBox1.Text = dataset.Tables("data").Rows(0)(2)
            ComboBox1.SelectedValue = dataset.Tables("data").Rows(0)(1)
            DateTimePicker1.Value = dataset.Tables("data").Rows(0)(4)
            DateTimePicker2.Value = dataset.Tables("data").Rows(0)(5)
            ComboBox2.SelectedValue = dataset.Tables("data").Rows(0)(6)
            RichTextBox1.Text = dataset.Tables("data").Rows(0)(8)
            RichTextBox2.Text = dataset.Tables("data").Rows(0)(3)
        Else
            Me.Text = "Nuevo Estudio"
            btn_editar.Hide()
        End If
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList
        qry = "select inve_id,(inve_nombre+' '+inve_apellido) as nombre from INVESTIGADORES"
        Consulta(qry, dataset)
        ComboBox1.DisplayMember = "nombre"
        ComboBox1.ValueMember = "inve_id"
        ComboBox1.DataSource = dataset.Tables("data")
        qry = "select pat_id,pat_nombre from patologias"
        Consulta(qry, dataset)
        ComboBox2.DisplayMember = "pat_nombre"
        ComboBox2.ValueMember = "pat_id"
        ComboBox2.DataSource = dataset.Tables("data")
    End Sub

    Private Sub btn_aceptar_Click(sender As System.Object, e As System.EventArgs) Handles btn_aceptar.Click
        If ValidarVacio(Me, Me.ErrorProvider1) = True Then
            qry = "insert into estudios values('" & ComboBox1.SelectedValue & "','" & TextBox1.Text & "','" & RichTextBox2.Text & "','" & DateTimePicker1.Value & "','" & DateTimePicker2.Value & "','" & ComboBox2.SelectedValue & "','1','" & RichTextBox1.Text & "')"
            EjecutaConsulta(qry)
            'registro movimiento
            registro("agregar", login.usuario.id, main.modulo.estudio)
            Me.Close()
            man_estudios.actualizar()
        End If
    End Sub

    Private Sub DateTimePicker2_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker2.ValueChanged
        If DateTimePicker1.Value.CompareTo(DateTimePicker2.Value) = 1 Then
            'MsgBox("Rango de fechas invalido")
            DateTimePicker1.Value = DateTime.Now()
            DateTimePicker2.Value = DateTime.Now()
        End If
    End Sub

    Private Sub btn_editar_Click(sender As System.Object, e As System.EventArgs) Handles btn_editar.Click
        If ValidarVacio(Me, Me.ErrorProvider1) = True Then
            Dim activo As String
            If RadioButton1.Checked = True Then
                activo = 1
            Else
                activo = 0
            End If
            qry = "update estudios set est_nombre='" & TextBox1.Text & "', inve_id='" & ComboBox1.SelectedValue & "',est_descripcion='" & RichTextBox2.Text & "',est_fecha_inicio='" & DateTimePicker1.Value & "',est_fecha_termino='" & DateTimePicker2.Value & "',pat_id='" & ComboBox2.SelectedValue & "',est_direccion='" & RichTextBox1.Text & "',est_estado='" & activo & "' where est_id='" & id & "'"
            EjecutaConsulta(qry)
            'registro movimiento
            registro("modificar", login.usuario.id, main.modulo.estudio)
            Me.Close()
            man_estudios.actualizar()
        End If
    End Sub

    Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub TextBox2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub RichTextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub RichTextBox2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox2.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

 
    Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        If DateTimePicker2.Value.CompareTo(DateTimePicker1.Value) = -1 Then
            'MsgBox("Rango de fechas invalido")
            DateTimePicker1.Value = DateTime.Now()
            DateTimePicker2.Value = DateTime.Now()
        End If
    End Sub
End Class